﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PIM_ATE.Mods
{
    public partial class FrmConfig : Form
    {
        public FrmConfig(DataRow row)
        {
            InitializeComponent();
            m_row = row;
        }

        public FrmConfig(DataRow row, string configVer,string specVer, string time, bool eff)
        {
            InitializeComponent();
            m_row = row;
            m_sConfigVer = configVer;
            m_sSpecVer = specVer;
            m_sEfftime = time;
            m_bEff = eff;
        }

        Label[] ctl_config_list_lblName;
        List<Control> ctl_config_list_tBItemValue;
        DataRow m_row;
        string m_sConfigVer;
        string m_sSpecVer;
        string m_sEfftime;
        bool m_bEff;

        private void FrmConfig_Load(object sender, EventArgs e)
        {
            createModule_config();

            if (m_row == null) return;

            for (int i = 0; i < m_row.ItemArray.Length; i++)
            {
                //ctl_config_list_tBItemValue[i].Text = m_row[i].ToString();
                if (ctl_config_list_tBItemValue[i].GetType() == typeof(TextBox))
                {
                    ctl_config_list_tBItemValue[i].Text = m_row[i].ToString();
                }

                else if (ctl_config_list_tBItemValue[i].GetType() == typeof(CheckBox))
                {
                    if (m_row[i].ToString() != "")
                        ((CheckBox)ctl_config_list_tBItemValue[i]).Checked = Convert.ToBoolean(m_row[i].ToString());
                }
                else if (ctl_config_list_tBItemValue[i].GetType() == typeof(ComboBox))
                {
                    if (m_row[i].ToString() != "")
                        ((ComboBox)ctl_config_list_tBItemValue[i]).Text = m_row[i].ToString();
                }
                    
            }

            ((CheckBox)ctl_config_list_tBItemValue[6]).CheckedChanged += FrmConfig_CheckedChanged;
            FrmConfig_CheckedChanged(ctl_config_list_tBItemValue[6], null);

            ctl_config_list_tBItemValue[26].Enabled = false;
            ctl_config_list_tBItemValue[27].Enabled = false;
            ctl_config_list_tBItemValue[28].Enabled = false;
            ctl_config_list_tBItemValue[29].Enabled = false;
        }

        void FrmConfig_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox checkObj = sender as CheckBox;
            ctl_config_list_tBItemValue[7].Enabled = !checkObj.Checked;
            ctl_config_list_tBItemValue[8].Enabled = !checkObj.Checked;
            ctl_config_list_tBItemValue[9].Enabled = !checkObj.Checked;
            ctl_config_list_tBItemValue[10].Enabled = checkObj.Checked;
            ctl_config_list_tBItemValue[11].Enabled = checkObj.Checked;
            ctl_config_list_tBItemValue[12].Enabled = checkObj.Checked;
            ctl_config_list_tBItemValue[13].Enabled = checkObj.Checked;

            if (checkObj.Checked)
            {
                ctl_config_list_tBItemValue[7].Text = "0";
                ctl_config_list_tBItemValue[8].Text = "0";
                ctl_config_list_tBItemValue[9].Text = "0";
            }
            else
            {
                ctl_config_list_tBItemValue[10].Text = "0";
                ctl_config_list_tBItemValue[11].Text = "0";
                ctl_config_list_tBItemValue[12].Text = "0";
                ctl_config_list_tBItemValue[13].Text = "0";
            }
        }

        void createModule_config()
        {
            List<string> names = ClsDataConfigBuilder.getConfigColNames();
            int count = names.Count - 2;
            ctl_config_list_lblName = new Label[count];

            float high = 100 / ctl_config_ptlControls.RowCount;
            for (int i = 0; i < ctl_config_ptlControls.RowCount; i++)
            {
                this.ctl_config_ptlControls.RowStyles[i] = new RowStyle(System.Windows.Forms.SizeType.Percent, high);
            }

            for (int i = 0; i < count; i++)
            {
                ctl_config_list_lblName[i] = new Label();
                ctl_config_list_lblName[i].Anchor = AnchorStyles.None;
                ctl_config_list_lblName[i].AutoSize = true;
                ctl_config_list_lblName[i].Location = new System.Drawing.Point(17, 6);
                ctl_config_list_lblName[i].Size = new System.Drawing.Size(47, 12);
                ctl_config_list_lblName[i].Text = names[i + 2] + ":";
            }

            ctl_config_list_tBItemValue = new List<Control>();
            for (int i = 0; i < count; i++)
            {
                if (ctl_config_list_lblName[i].Text.Contains("PIM类型"))
                {
                    ComboBox cb = new ComboBox();
                    cb.DropDownStyle = ComboBoxStyle.DropDownList;
                    cb.Items.Add("带内互调");
                    cb.Items.Add("带外互调");
                    cb.SelectedIndex = 0;
                    ctl_config_list_tBItemValue.Add(cb);

                }
                else if (ctl_config_list_lblName[i].Text.Contains("是否扫频") || ctl_config_list_lblName[i].Text.Contains("是否敲击"))
                {
                    CheckBox check = new CheckBox();
                    check.Checked = true;
                    ctl_config_list_tBItemValue.Add(check);
                }
                else if (ctl_config_list_lblName[i].Text.Contains("单位"))
                {
                    ComboBox cb = new ComboBox();
                    cb.DropDownStyle = ComboBoxStyle.DropDownList;
                    cb.Items.Add("dBm");
                    cb.Items.Add("dBc");
                    cb.SelectedIndex = 0;
                    ctl_config_list_tBItemValue.Add(cb);
                }
                else if (ctl_config_list_lblName[i].Text.Contains("阶数"))
                {
                    ComboBox cb = new ComboBox();
                    cb.DropDownStyle = ComboBoxStyle.DropDownList;
                    cb.Items.Add("3");
                    cb.Items.Add("5");
                    cb.Items.Add("7");
                    cb.Items.Add("9");
                    cb.Items.Add("11");
                    cb.Items.Add("3-H");
                    cb.Items.Add("5-H");
                    cb.Items.Add("7-H");
                    cb.Items.Add("9-H");
                    cb.Items.Add("11-H");
                    cb.Items.Add("13-H");
                    cb.SelectedIndex = 0;
                    ctl_config_list_tBItemValue.Add(cb);
                }
                else if (ctl_config_list_lblName[i].Text.Contains("测试频段"))
                {
                    ComboBox cb = new ComboBox();
                    cb.DropDownStyle = ComboBoxStyle.DropDownList;
                    cb.Items.Add("700M");
                    cb.Items.Add("800M");
                    cb.Items.Add("900M");
                    cb.Items.Add("1800M");
                    cb.Items.Add("1900M");
                    cb.Items.Add("2100M");
                    cb.Items.Add("2300M");
                    cb.Items.Add("2600M");
                    cb.SelectedIndex = 0;
                    ctl_config_list_tBItemValue.Add(cb);
                }
                else
                {
                    TextBox text = new TextBox();
                    text.Anchor = AnchorStyles.Left | AnchorStyles.Right;
                    ctl_config_list_tBItemValue.Add(text);
                }
            }

            for (int j = 0, n = 0; j < ctl_config_ptlControls.RowCount; j++)
            {
                for (int i = 0; i < ctl_config_ptlControls.ColumnCount && n < count; i += 2)
                {
                    if (ctl_config_list_lblName[n].Text == "Message:" || ctl_config_list_lblName[n].Text == "Remark:")
                    {
                        if ((j * i) % 4 == 0)
                        {
                            ctl_config_ptlControls.Controls.Add(ctl_config_list_lblName[n], i, j);
                            ctl_config_ptlControls.Controls.Add(ctl_config_list_tBItemValue[n], i + 1, j);
                            ctl_config_ptlControls.SetColumnSpan(ctl_config_list_tBItemValue[n], 3);
                            n++;
                        }

                        i += 2;
                    }
                    else
                    {
                        ctl_config_ptlControls.Controls.Add(ctl_config_list_lblName[n], i, j);
                        ctl_config_ptlControls.Controls.Add(ctl_config_list_tBItemValue[n], i + 1, j);
                        n++;
                    }
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                int n1 = ((CheckBox)ctl_config_list_tBItemValue[6]).Checked ? 10 : 7;
                int n2 = ((CheckBox)ctl_config_list_tBItemValue[6]).Checked ? 13 : 9;

                for (int i = 0; i < m_row.ItemArray.Length; i++)
                {
                    if (m_row.Table.Columns[i].DataType == typeof(DateTime))
                    {
                        if (ctl_config_list_tBItemValue[i].Text == "")
                        {
                            MessageBox.Show(ctl_config_list_lblName[i].Text + "     空！");
                            return;
                        }
                        m_row[i] = DateTime.Parse(ctl_config_list_tBItemValue[i].Text);
                    }
                    else
                    {
                        //m_row[i] = ctl_config_list_tBItemValue[i].Text;
                        if (ctl_config_list_tBItemValue[i].GetType() == typeof(TextBox))
                        {
                            if (i >= n1 && i <= n2 && ctl_config_list_tBItemValue[i].Text == "0")
                            {
                                MessageBox.Show(ctl_config_list_lblName[i].Text + "     为0");
                                return;
                            }

                            if (i != 30 && ctl_config_list_tBItemValue[i].Text == "")
                            {
                                MessageBox.Show(ctl_config_list_lblName[i].Text + "     空");
                                return;
                            }

                            if (ctl_config_list_lblName[i].Text.Contains("测试要求"))
                            {
                                double spec;
                                if (double.TryParse(ctl_config_list_tBItemValue[i].Text, out spec))
                                {
                                    m_row[i] = "[-INF," + ctl_config_list_tBItemValue[i].Text + "]";
                                }
                                else
                                {
                                    m_row[i] = ctl_config_list_tBItemValue[i].Text;
                                }
                            }
                            else
                            {
                                m_row[i] = ctl_config_list_tBItemValue[i].Text;
                            }
                        }
                        else if (ctl_config_list_tBItemValue[i].GetType() == typeof(CheckBox))
                            m_row[i] = ((CheckBox)ctl_config_list_tBItemValue[i]).Checked.ToString();
                        else if (ctl_config_list_tBItemValue[i].GetType() == typeof(ComboBox))
                            m_row[i] = ((ComboBox)ctl_config_list_tBItemValue[i]).Text;
                    }
                        
                }

                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
